package org.myblog.myblogserver.code.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.myblog.myblogserver.code.entity.Admin;
import org.myblog.myblogserver.code.entity.User;
import org.myblog.myblogserver.code.service.AdminService;
import org.myblog.myblogserver.dao.Result;
import org.myblog.myblogserver.dao.UserDao;
import org.myblog.myblogserver.dao.UserLoginDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;

@Api(tags = "Admin")
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    private Result result = new Result();



    /**
     * post  /admin/login<br/>
     * json<br/>
     *
     * @author 智哥
     * @param admin
     * @return Result
     * */
    @PostMapping("/login")
    @ApiOperation(value = "Admin登录")
    public Result login(@RequestBody Admin admin) {
        System.out.println("admin = " + admin);
        UserLoginDao login = adminService.login(admin.getUsername(), admin.getPassword());
        if(login!=null){
            result.success("登录成功", login);
        }
        else {
            result.client_fail("用户名密码错误", null);
        }
        return result;
    }

    /**
     * post  /admin/sign<br/>
     * json<br/>
     *
     * @author 智哥
     * @param admin
     * @return Result
     * */
    @PostMapping("/sign")
    @ApiOperation(value = "Admin注册")
    public Result sign(@RequestBody Admin admin) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", admin.getUsername());
        Admin user =adminService.getOne(queryWrapper);
        if(user != null){
            result.client_fail("用户已存在", null);
            return result;
        }
        if(adminService.sign(admin)){
            result.success("注册成功", null);
        }
        else{
            result.client_fail("注册失败", null);
        }
        return result;
    }



    /**
     * post  /admin/deleteAll<br/>
     *
     * @author 智哥
     * @return Result
     * */
    @ApiOperation(value = "Admin删除全部")
    @PostMapping("/deleteAll")
    public Result deleteAll() {
        if(adminService.remove(null)){
            result.success("删除成功", true);
        }
        else{
            result.server_fail("删除失败", false);
        }
        return result;
    }
}